/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package trainmodel;

/**
 *
 * @author Yan
 */

import java.util.*;

public class Train<T> {
    int carNum;
    int trainID;
    double power;
    double force;
    double acceleration;
    Cars[] cars;
    long startTime;
    double speed;
    int authority;
    double speedLimit;
    double currentSpeed;
    double maxPower=120000;
    double friction;
    double coefFriction=0.0122249389;//5000/409000==>friction/normal force


    

    public Train(int numOfcars, int ID){
        startTime = System.currentTimeMillis();
        cars = new Cars[numOfcars];
        Engine e = new Engine();
        trainID = ID;
        carNum = numOfcars;
        e.currentSpeed= 0;
        e.startT = startTime;
        
        for(int i=0;i<carNum;i++){
            cars[i] = new Cars(i);
        }
    }

//P=F*v
//F=m*a
    double setAcceleration(double acc){//this method will be called every mili second
        double actual_acc=getActualAcceleration(acc);
        currentSpeed=currentSpeed+0.001*actual_acc;

        return currentSpeed;
    }




    double getActualAcceleration(double acc_commanded){
        double gravity = 9.98;
        double externalForce = 0;
        double rise;
        double run=100;
        double rampL;
        double acc;
        double mass =0;

        double slope = 0;
        double sin_angle;

        //RailInterfo info = headblockinfo(trainID);
        //slope= into.slope;
        rise = run * slope;
        rampL = Math.sqrt(rise * rise + run*run);
        sin_angle = rise/rampL;
        for(int i = 0; i < carNum; i++){
            mass+=cars[i].getTotalMass();
        }
        friction = mass*gravity*coefFriction;

        double controllerForce = acc_commanded*mass;
        externalForce = sin_angle * mass* gravity + friction;
        force = controllerForce - externalForce;
        acc = force/mass;
        return acc;

    }

    int getID(){
        return trainID;
    }

    int getTrainLength(){
        int totalLength = 0;
        for(int i = 0; i < carNum; i++){
            totalLength+=cars[i].getLength();
        }
        return totalLength;
    }

    int getMidPoint(){
        int full = 0;
        for(int i = 0; i < carNum; i++){
            full+=cars[i].getLength();
        }
        return full/2;

    }

    void startTrain(){

    }

    void shutDownTrain(){
        
    }

    void makeTrain(){
        makeTrain(5);

    }
    void makeTrain(int cars){        
        initializeCar();

    }

    void initializeCar(){
        

    }

    
     void setAuthority(int auth){
        authority = auth;
    }
    int getAuthority(){
        return authority;
    }
    void setSpeedLimit(double speed){
        speedLimit = speed;
    }
    double getSpeedLimit(){
        return speedLimit;
    }

    double getCurrentSpeed(){

       return currentSpeed;
    }

}
